package com.zyz.forum.controller;

import com.zyz.forum.common.AppResult;
import com.zyz.forum.common.ResultCode;
import com.zyz.forum.config.AppConfig;
import com.zyz.forum.model.Article;
import com.zyz.forum.model.ArticleReply;
import com.zyz.forum.model.User;
import com.zyz.forum.service.impl.ArticleReplyServiceImp;
import com.zyz.forum.service.impl.ArticleServiceImp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "回复接口")
@RestController
@RequestMapping("/articleReply")
public class ArticleReplyController {

    @Resource
    private ArticleReplyServiceImp articleReplyService;
    @Resource
    private ArticleServiceImp articleService;


    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复内容") @RequestParam("content") @NonNull String content) {
        // 获取session
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_ID);
        // 判断user状态
        if (user.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 判断文章状态
        Article article = articleService.selectById(articleId);
        if (article == null) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getDeleteState() == 1 || article.getState() == 1) {
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS_OR_NO_MODIFICATION.toString());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS_OR_NO_MODIFICATION);
        }
        // 构造回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setContent(content);
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        // 调用service层
        articleReplyService.create(articleReply);
        // 新增成功
        return AppResult.success();

    }

    @ApiOperation("根据帖子id获取所有回复信息")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        // 文章状态校验
        Article article = articleService.selectById(articleId);
        if (article == null) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1 || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS_OR_NO_MODIFICATION);
        }
        // 调用service层
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        // 返回结果
        return AppResult.success(articleReplies);
    }


}
